﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CommTest
{
    public partial class IMUBoard : Form
    {
        MainForm.SDataStructure lastData;

        public IMUBoard()
        {
            InitializeComponent();
        }

        public void NewData(MainForm.SDataStructure copyStruct)
        {
            lastData = copyStruct;
        }

        private void timerGUI_Tick(object sender, EventArgs e)
        {
            // update GUI
            textBoxGyroX.Text = string.Format("{0:f2} °/s", lastData.GyroX * 180 / Math.PI);
            textBoxGyroY.Text = string.Format("{0:f2} °/s", lastData.GyroY * 180 / Math.PI);
            textBoxGyroZ.Text = string.Format("{0:f2} °/s", lastData.GyroZ * 180 / Math.PI);

            textBoxRoll.Text = string.Format("{0:f2} °", lastData.Roll * 180 / Math.PI);
            textBoxPitch.Text = string.Format("{0:f2} °", lastData.Pitch * 180 / Math.PI);
            textBoxYaw.Text = string.Format("{0:f2} °", lastData.Yaw * 180 / Math.PI);

            textBoxPressureABS.Text = string.Format("{0:f3} kPa", lastData.AbsPressure/1000);
            textBoxPressureDiff.Text = string.Format("{0:f0} Pa", lastData.DiffPressure);
            textBoxTemperature.Text = string.Format("{0:f2} °C", lastData.IMUTemperature);
            double baroAltitude = 44330 * (1 - Math.Pow((lastData.AbsPressure / 101325), 1 / 5.255));
            textBoxAltitudeBaro.Text = string.Format("{0:f2} m", baroAltitude);
            double pitotSpeed = Math.Sqrt(2*Math.Abs(lastData.DiffPressure)/1.22521);
            if (lastData.DiffPressure < 0)
            {
                pitotSpeed = -pitotSpeed;
            }
            textBoxAirSpeed.Text = string.Format("{0:f2} kph", pitotSpeed*3.6);


            textBoxLoop.Text = string.Format("{0:f2} ms", lastData.IMULoop);
            textBoxLoopMAX.Text = string.Format("{0:f2} ms", lastData.IMULoopMAX);
            textBoxTimeTotal.Text = string.Format("{0:f2} s", lastData.IMUTotalTime);

            textBoxGPSSatNr.Text = string.Format("{0:f1} ", lastData.GPSSatNr);
            textBoxLongitude.Text = string.Format("{0:f6} °", lastData.GPSLongitude);
            textBoxLatitude.Text = string.Format("{0:f6} °", lastData.GPSLatitude);
            textBoxAltitudeGPS.Text = string.Format("{0:f2} m", lastData.GPSAltitude);
        }
    }
}
